<template>
  <!-- QQ飞车自动获取授权 -->
  <el-dialog :title="'扫码授权'" :visible.sync="dialogVisible" :close-on-click-modal="false" width="300px" append-to-body @close="close">
    <div class="form-box" v-loading="loading">
      <div class="demo-ruleForm">
        <img :src="qrCode" alt="" style="width: 200px; height: 200px" />
        <div class="center">
          请扫描二维码进行授权
        </div>
        <div><el-button style="width: 150px;" @click="checkQrcode" type="primary">我已授权</el-button></div>
      </div>
    </div>
  </el-dialog>
</template>
  
  <script>
import {
  QQspeedGetCode,
  QQspeedCheckCode,
  QQspeedGetData,
} from "../../api/autoProduction/information";
export default {
  name: "QQspeed",
  props: [""],
  data() {
    return {
      dialogVisible: false,
      loading: false,
      checkPending: false,
      msg: "",
      qrCode: "",
      uuid: "",
      carrierId: "",
      timeOutTimer: null,
      loginBody: {},
      type:'QQ飞车',
      qufu:'qq'
    };
  },
  created() {},
  beforeDestroy() {
    this.closeTimer();
  },
  methods: {
    // 打开弹窗
    open(type='QQ飞车',qufu) {
      this.dialogVisible = true;
      this.loading = false;
      this.type = type;
      this.qufu = qufu;
      this.qrCode = "";
      this.getQrCode();
    },
    close() {
      this.closeTimer();
    },
    getQrCode() {
      this.loading = true;
      this.closeTimer();
      const obj = {
        type: this.qufu,
      };
      QQspeedGetCode(obj)
        .then((res) => {
          this.loading = false;
          if (res.code == 200&&res.data.data&&res.data.data.qr_code) {
            this.qrCode =  res.data.data.qr_code;
            this.uuid = res.data.data.sessionId;
            this.msg = "";
            if (!this.checkPending) this.checkQrcode();
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 关闭延时器
    closeTimer() {
      if (this.timeOutTimer) clearTimeout(this.timeOutTimer);
      this.timeOutTimer = null;
    },
    // 校验二维码状态
    async checkQrcode() {
      let res;
      this.checkPending = true;
      try {
          res = await QQspeedCheckCode({
            sessionId: this.uuid,
          });
        } catch (e) {
          if (this.dialogVisible) {
            this.getQrCode();
          }
        }
      this.checkPending = false;
      this.closeTimer();
      if (res && res.code) {
        if (
          this.dialogVisible &&
          (res.data.data.message == '等待QQ扫码登录中...'||res.data.data.message =='等待微信扫码登录中...')
        ) {
          // 延时1秒后查询二维码状态
          this.timeOutTimer = setTimeout(() => {
            if (this.dialogVisible) {
              this.checkQrcode();
            }
          }, 2000);
          
        } else if (
          this.dialogVisible &&(res.data.data.message == 'QQ登录成功'||res.data.data.message == '微信登录成功')
        ) {
          // 已授权  开始获取数据
          this.loginBody = res.data.data.user_info
          this.getInfo();
        }
      }
    },
    async getInfo() {
      this.loading = true;
      let res;
      let params = {
        ...this.loginBody,
        "gameType":this.type,
      }
      try {
        res = await QQspeedGetData(params);
      } catch (e) {
        this.getQrCode();
      }
      this.loading = false;
      this.closeTimer();
      if (res.data&&res.data.data) {
        // console.log("%c Line:155 🍫 res.data", "color:#3f7cff", res.data);
        this.$emit("success", res.data.data);
        this.dialogVisible = false;
      }else{
        this.$message.error('获取失败！')
        this.dialogVisible = false;
      }
    },
    extractUrlFromString(str) {
      // 匹配以http或https开头的URL
      const urlPattern = /https?:\/\/[^\s',]+/;
      const match = str.match(urlPattern);
      return match ? match[0] : null;
    },

    getWXloginCode(str) {
      const regex = /wx_code='([^']+)'/; // 匹配单引号内的内容
      const match = str.match(regex);
      const wxCode = match ? match[1] : null; // 提取捕获组
      return wxCode;
    },
  },
};
</script>
  <style scoped lang="css">
.demo-ruleForm {
  text-align: center;
}
</style>
  